<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%layout("/WEB-INF/view/common/inc/admin_head.html",{title:'', styles:'bootstrap,font-awesome,animate,style,bootstrap-table,layui-date,auto-complete,dtvalidate,layui'}){%>
    <style type="text/css">
        body{padding-top:10px;}
        *{margin:0;padding:0;list-style-type:none;}
        a,img{border:0;}
        .demo h2{font-size:16px;color:#3366cc;height:30px;}
        .demo li{float:left;}
    </style>
    <%}%>
</head>

<body>
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">

            <!-- START 右侧数据 -->

            <!-- START 操作按钮 -->
            <!-- END 操作按钮 -->
            <div id="toolbar" class="btn-group" style="margin-bottom:5px;">
                <button  class="layui-btn layui-btn-sm import-btn" style="margin-top: -6px;margin-left: -4px" id="import-btn"  name="import-btn">
                    <i class="layui-icon"></i>
                    批量修改商品信息
                </button>
                &nbsp;
                <button class="download-pdf-btn layui-btn  layui-btn-sm"  style="margin-top: -6px;margin-left: -4px">
                    <i class="fa fa-download" aria-hidden="true"></i> 下载合并PDF文件</a>
                </button>

            </div>
            <!-- START 数据表格 -->
            <table id="planTable"
                   data-toolbar="#toolbar"
                   data-pagination="true"
                   data-resizable="true"
                   data-side-pagination="server"
                   data-url="/sea/pdfSku/findByCnd.do"
                   data-page-size="15"
                   class="table table-striped table-hover"
            >
            </table>
            <!-- END 数据表格 -->

            <!-- END 右侧数据 -->
        </div>
    </div>
</div>

<%layout("/WEB-INF/view/common/inc/admin_js.html",{modules:'jquery,bootstrap,bootstrap-table,dtvalidate,dtvalidate-lang-zh_cn,bootstrap-table-zh-CN,laydate,layer,layui'}){}%>
<script>
    var $planTable = $('#planTable');
    $(function () {
        $('.download-pdf-btn').on('click',function () {
            var selDatas = $planTable.bootstrapTable('getSelections');
            console.log(selDatas);
            if( selDatas.length == 0 ){
                layer.msg('请选择要导出的',{icon: 0,shade: [0.8, '#393D49'],time: 1500});
            }else{
                var ids = [],names = [];
                $.each(selDatas,function(i,d){
                    ids.push(d.id);
                });
                ids = ids.join(',');

                var index= layer.load(2, { //icon支持传入0-2
                    shade: [0.1, '#fff'], //0.5透明度的灰色背景
                    content: '正在生成压缩包,请稍后...',
                    success: function (layero) {
                        layero.find('.layui-layer-content').css({
                            'padding-top': '39px',
                            'margin-left': '-50px',
                            'width': '300px',
                            'font-color': 'red',
                            'font-size': '20px'
                        });
                    }
                });
                $.ajax({
                    url: "/sea/pdfSku/downloadPdf.do?ids="+ids,
                    type: "post",
                    success: function (data) {
                        if (data.ok) {
                            var link = document.createElement('a');
                            link.href = data.msg;
                            link.setAttribute('target', '_blank');
                            link.setAttribute('download', '');
                            link.download = ''; // 可选：如果设置文件名，可以在此指定
                            document.body.appendChild(link);
                            link.click();
                            document.body.removeChild(link);
                            layer.closeAll();
                        } else {
                            layer.msg(data.msg, {
                                icon: 0, btn: ['确定'], time: 0, shade: [0.8, '#393D49'], yes: function (index, layero) { // 默认的是 按钮一
                                    layer.closeAll();
                                }
                            });
                        }
                    },
                });
            }

        });


        layui.use('upload', function(){
            var upload = layui.upload;
            upload.render({
                elem: '#import-btn', //绑定元素,
                accept:'file',
                url: '/sea/pdfSku/importPdfSku/', //上传接口
                before:function () {
                    layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                }
                ,done: function(data){
                    if( data.ok ){
                        layer.msg(data.msg,{icon: 1,time: 1500},function(){
                            //刷新页面
                            $planTable.bootstrapTable('refresh');
                            layer.closeAll();
                        });
                    }else{
                        layer.msg(data.msg,{icon: 0,time: 5000},function () {
                            layer.closeAll();
                        });

                    }
                }
                ,error: function(){
                    //请求异常回调
                    layer.closeAll();
                }
            });
        });

        // START 表格配置
        $planTable.bootstrapTable({
            pageSize:10,
            columns: [
                {
                    field: 'state',
                    checkbox: true,
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: 'SKU',
                    field: 'pdfPlanNo',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '附件',
                    field: 'pdfFile',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        if(null!=v && v!=''){
                            var result='<button class=" download-btn layui-btn  layui-btn-sm"  style="margin-top: -6px;margin-left: -4px">';
                            result+='<i class="fa fa-download" aria-hidden="true"></i> <a href="'+v+'" download style="color: white"  target="_blank" download>下载PDF文件</a>';
                            result+='</button>';
                            return result;
                        }else{
                            return '';
                        }
                    }
                }
            ],
            onLoadSuccess : function(){

                $('.detail-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    layer.open({
                        type: 2,
                        area: ['700px','500px'],
                        content: ['/sea/pdf/record.do?createPlanNo='+id, 'yes']
                    });
                });

            }
        });

    });

    // 操作菜单
    var status;


    $('.search-btn').on('click',function(){
        $planTable.bootstrapTable('refresh', { url:'${ctxPath}/sea/physical/distribute/findByCnd.do?'+$('#search-form').serialize() });
    });

</script>
</body>
</html>
